<template>
  <el-card class="container">
    <template #header>挂号订单 </template>
    <!-- 搜索条件   -->
    <el-form :inline="true" class="form">
      <el-form-item label="就诊人">
        <el-select v-model="patientId" :clearable="true" style="width: 240px">
          <el-option
            v-for="item in visitingUserList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="orderStatus" :clearable="true" style="width: 240px">
          <el-option
            v-for="item in orderStatusList"
            :key="item.status"
            :label="item.comment"
            :value="item.status"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          style="margin: 10px; width: 80px"
          type="primary"
          @click="getOrderList"
          >查询</el-button
        >
        <el-button style="margin: 10px; width: 80px" @click="resetForm"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!--  列表  -->
    <el-table
      :data="orderList"
      :row-key="
        (row) => {
          row.id;
        }
      "
      height="500"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="就诊时间" width="120">
        <template #default="scope">{{ scope.row.reserveDate }}</template>
      </el-table-column>
      <el-table-column label="医院" property="hosname" width="150" />
      <el-table-column label="科室" property="depname" width="240" />
      <el-table-column el="医生" lab property="title" width="100" />
      <el-table-column label="医生服务费" property="amount" width="100" />
      <el-table-column label="就诊人" property="patientName" width="100" />
      <el-table-column label="窗口" property="fetchAddress" width="120" />
      <el-table-column
        label="订单状态"
        property="param.orderStatusString"
        width="150"
      />
      <el-table-column fixed="right" label="操作" width="100">
        <template #="{ row }" #default>
          <el-button link type="primary" @click="toDetail(row.id)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="page"
      v-model:page-size="limit"
      :background="true"
      :page-sizes="[10, 20, 30, 40]"
      :total="total"
      class="page"
      layout="  prev, pager, next, jumper,sizes,total,"
      @size-change="getOrderList"
      @current-change="getOrderList"
    />
  </el-card>
</template>
<script lang="ts" name="OrderList" setup>
import { onMounted, ref } from "vue";
import { doGetOrderList, doGetOrderStatus } from "@/api/order.ts";
import { Order, OrderStatus } from "@/types/domain/order.ts";
import router from "@/router";
import { doGetAllUser } from "@/api/user.ts";
import { VisitingUser } from "@/types/types.ts";

let page = ref<number>(1);
let limit = ref<number>(10);
let total = ref<number>(0);
let pages = ref<number>(0);
// 就诊人
let patientId = ref<number>();
// 订单状态
let orderStatus = ref<number>();

// 就诊人列表
let visitingUserList = ref<VisitingUser[]>([]);

// 列表数据
let orderList = ref<Order[]>([]);
// 订单状态
let orderStatusList = ref<OrderStatus[]>([]);
onMounted(() => {
  // 就诊人
  getAllUser();
  // 订单状态
  getOrderStatus();
  // 获取订单列表
  getOrderList();
});
// 获取订单列表
const getOrderList = async () => {
  console.log("参数", patientId.value, orderStatus.value);
  let res = await doGetOrderList(
    page.value,
    limit.value,
    patientId.value,
    orderStatus.value,
  );
  orderList.value = res.records;
  page.value = res.current;
  limit.value = res.size;
  total.value = res.total;
  pages.value = res.pages;
};
// 重置查询条件
const resetForm = () => {
  patientId.value = "";
  orderStatus.value = "";
  getOrderList();
};
// 跳转到详情页
const toDetail = (orderId: number) => {
  router.push({
    path: "/user/order",
    query: {
      orderId,
    },
  });
};
// 获取所有就诊人
const getAllUser = async () => {
  visitingUserList.value = await doGetAllUser();
};
// 订单状态
const getOrderStatus = async () => {
  orderStatusList.value = await doGetOrderStatus();
};
</script>

<style lang="scss" scoped>
.container {
  width: 950px;
  margin-left: 20px;
  margin-top: 50px;
  margin-bottom: 50px;

  .form {
    width: 100%;
  }
  .page {
    margin-top: 20px;
  }
}
</style>
